using UnityEngine;

[ExecuteInEditMode]
public class ColorChanger : MonoBehaviour
{
	private Color _color;

	public Color MeshColor;

	private MeshFilter _meshFilter;

	public Mesh CachedSharedMesh;

	[HideInInspector]
	public Color[] Colors;

	[HideInInspector]
	public UVOffsetTracker UVOffsetTracker;

	private void Start()
	{
		if (!Application.isPlaying)
		{
			CachedSharedMesh = null;
		}
		UVOffsetTracker = GetComponent<UVOffsetTracker>();
		if (UVOffsetTracker != null)
		{
			UVOffsetTracker.ColorChanger = this;
		}
		_meshFilter = GetComponent<MeshFilter>();
		_color = MeshColor;
		if (!base.gameObject.isStatic)
		{
			CheckMesh();
			UpdateColor();
		}
	}

	private void CheckMesh()
	{
		if (_meshFilter != null && _meshFilter.sharedMesh == null)
		{
			MeshCollider component = GetComponent<MeshCollider>();
			if (component != null)
			{
				_meshFilter.sharedMesh = component.sharedMesh;
			}
		}
		if (CachedSharedMesh == null && _meshFilter != null)
		{
			CachedSharedMesh = ((!(UVOffsetTracker != null)) ? _meshFilter.sharedMesh : UVOffsetTracker.CachedSharedMesh);
		}
	}

	private void UpdateColor()
	{
		if (!base.gameObject.isStatic)
		{
			if (_meshFilter == null)
			{
				_meshFilter = GetComponent<MeshFilter>();
			}
			Mesh cachedSharedMesh = CachedSharedMesh;
			Mesh mesh = new Mesh();
			mesh.vertices = cachedSharedMesh.vertices;
			mesh.normals = cachedSharedMesh.normals;
			mesh.tangents = cachedSharedMesh.tangents;
			mesh.uv = ((!(UVOffsetTracker == null)) ? UVOffsetTracker.CurrentUV : cachedSharedMesh.uv);
			mesh.uv2 = ((!(UVOffsetTracker == null)) ? UVOffsetTracker.CurrentUV : cachedSharedMesh.uv2);
			mesh.uv2 = cachedSharedMesh.uv2;
			mesh.triangles = cachedSharedMesh.triangles;
			Colors = new Color[CachedSharedMesh.vertices.Length];
			for (int i = 0; i < Colors.Length; i++)
			{
				Colors[i] = _color;
			}
			mesh.colors = Colors;
			_meshFilter.mesh = mesh;
		}
	}

	public void SetColor(Color iColor)
	{
		if (iColor != _color)
		{
			_color = iColor;
			MeshColor = iColor;
			UpdateColor();
		}
	}
}
